package com.ardo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ardo.entity.ResponseResult;
import com.ardo.entity.mall.WxLoginLog;
import com.ardo.entity.mall.WxUser;
import com.ardo.entity.vo.User;
import com.ardo.mapper.UserMapper;
import com.ardo.service.UserService;
import com.ardo.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void insertWxUser(WxUser user) {
        userMapper.insertWxUser(user);
    }

    @Override
    public WxUser findByOpenid(String openid) {
        System.out.println("查询的openid="+openid);
        return userMapper.findByOpenid(openid);
    }

    @Override
    public void insertWxLoginLog(WxLoginLog log) {
        userMapper.insertWxLoginLog(log);
    }

    @Override
    public String userDetail(String token) {
        String result = "";
        String openid = "";
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("token->"+token);
        Map<String,Object> validMap = JwtUtil.validateToken(token);
        if(validMap.get("openid")!=null){
            openid = validMap.get("openid") + "";
            System.out.println("token中识别openid -> " + openid);
            WxUser wxuser = userMapper.findByOpenid(openid);
            if(wxuser!=null){
                User vo = new User();
                vo.setNick(wxuser.getName());
                vo.setAvatarUrl(wxuser.getAvatarUrl());
                vo.setMobile(wxuser.getMobile());
                map.put("base", vo);
                result = JSONObject.toJSONString(ResponseResult.success(map));
            }
        }

        return result;
    }

    @Override
    public String amount(String token) {
        String result = "";
        String openid = "";
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String,Object> validMap = JwtUtil.validateToken(token);
        if(validMap.get("openid")!=null){
            openid = validMap.get("openid") + "";

            map.put("balance", 66.00);
            map.put("freeze", 12.00);
            map.put("score", 88.00);
            map.put("growth", 36.00);

            result = JSONObject.toJSONString(ResponseResult.success(map));
        }
        return result;
    }
}
